/**
 * Created by Administrator on 2015/12/30.
 */
"use strict";

/**优惠券管理API
 * act:1: 后台优惠券管理列表 ,2:新增优惠券；3：更新优惠券；4：优惠券详情；
 * 5：新增优惠券领取记录；6：更新一条优惠券记录；7：领取记录列表；
 * 8：优惠券领取记录详情
 * */
var F = require('../../../common/function');
var _ = require('underscore');

exports.couponOperation = function *(Env) {
  let fields = Env.I;
  let act = fields.act;
  let loginStatus = fields.loginStatus;
  //身份验证
  if (!loginStatus.status) {
    return F.returnMsg('401', '身份验证失败', 3);
  }
  let id = fields.id;
  if (_.v.isIn(act, [3, 4, 5, 6,8])) {
    if (id == undefined || !_.v.isInt(id)) {
      return F.returnMsg(400, '请先传入id参数', 1);
    }
  }
  id = parseInt(id);
  let coupon_info = _.pick(fields, 'type', 'vendorid', 'query', 'onLineTime', 'offLineTime');
  let coupon = require("./mCoupon");

  //后台优惠券列表
  if (act == 1) {
    coupon_info.type = loginStatus.vendorType;//1.普通券（总后台）；2.商家券(商家后台)
    coupon_info.vendorid = loginStatus.vid;
    let m_coupon = new coupon.mCoupon(coupon_info);
    return yield m_coupon.query();
  } else if (act == 2) {//新增优惠券
    let coupon_info = _.pick(fields, 'name', 'money', 'circulation', 'applicableGte', 'applicableUser', 'onLineTime', 'offLineTtime');
    coupon_info.type = loginStatus.vendorType;//1.普通券（总后台）；2.商家券(商家后台)
    coupon_info.vendorid = loginStatus.vid;
    let m_coupon = new coupon.mCoupon(coupon_info);
    return yield m_coupon.add();
  } else if (act == 3) {//更新优惠券
    let coupon_info = _.pick(fields, 'id', 'name', 'money', 'circulation', 'applicableGte', 'applicableUser', 'onLineTime', 'offLineTtime', 'provide', 'valid', 'status');
    let m_coupon = new coupon.mCoupon(coupon_info);
    return yield m_coupon.update();
  } else if (act == 4) {//优惠券详情
    let coupon_info = {id: id};
    let m_coupon = new coupon.mCoupon(coupon_info);
    return yield m_coupon.detail();
  } else if (act == 5) {//新增优惠领取记录
    let coupon_info = _.pick(fields, 'uid', 'id');
    let m_coupon = new coupon.mCoupon(coupon_info);
    return yield m_coupon.addLog();
  } else if (act == 6) {//更新优惠券记录
    let coupon_info = _.pick(fields, 'id', 'status');
    let m_coupon = new coupon.mCoupon(coupon_info);
    return yield m_coupon.updateLog();
  } else if (act == 7) {//优惠券领取记录列表
    let coupon_info = _.pick(fields,'uid', 'id','page', 'pageSize', 'vendorid');
    let m_coupon = new coupon.mCoupon(coupon_info);
    return yield m_coupon.logQuery();
  } else if (act == 8) {//优惠券详情
    let coupon_info = _.pick(fields, 'id');
    let m_coupon = new coupon.mCoupon(coupon_info);
    return yield m_coupon.logDetail();
  }
};